$(function () {
    $("#jqGrid").jqGrid({
        url: '../hesuser/list',     // 请求后台json数据的url
        datatype: "json",                // 后台返回的数据格式
        // 列表标题及列表模型
        colModel: [
            {label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true},
            {label: '用户名', name: 'username', index: 'username', width: 80},
            {label: '密码', name: 'password', index: 'password', width: 80},
            {label: '真实姓名', name: 'truename', index: 'trueName', width: 80},
            {label: '手机号', name: 'mobile', index: 'mobile', width: 80},
            {label: '邮箱', name: 'email', index: 'email', width: 80},
            {label: '用户部门', name: 'dept', index: 'dept', width: 80},
            {label: '职务', name: 'job', index: 'job', width: 80},
            {label: '视频电话号码', name: 'zwannumber', index: 'zwannumber', width: 80},
            {label: '视频电话密码', name: 'zwanpassword', index: 'zwanpassword', width: 80},
            {label: '城市编码', name: 'citycode', index: 'citycode', width: 80},
            {label: '擅长设备', name: 'adept', index: 'adept', width: 80},
            {label: '唯一识别码', name: 'mac', index: 'mac', width: 80},
            {label: '创建者ID', name: 'createUserId', index: 'create_user_id', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80}
        ],
        viewrecords: true,     // 是否显示行号，默认值是false，不显示
        height: 385,            // 表格高度
        rowNum: 50,             // 一页显示的行记录数
        rowList: [50, 100],     // 翻页控制条中 每页显示记录数可选集合
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",          // 翻页DOM
        jsonReader: {                   // 重写后台返回数据的属性
            root: "page.list",          // 将rows修改为page.list
            page: "page.currPage",      // 将page修改为page.currPage
            total: "page.totalPage",    // 将total修改为page.totalPage
            records: "page.totalCount"  // 将records修改为page.totalCount
        },
        prmNames: {              // 改写请求参数属性
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            //设置高度
            $("#jqGrid").jqGrid('setGridHeight', getWinH());
        }
    });

    $.get("../hesexpert/provinces/", function (r) {
        // vm.provinces = r.region;
        var data = r.provinces;
        var optionstring = "";
        for (var i in data) {
            var jsonObj = data[i];

            optionstring += "<option value=\"" + jsonObj.id + "\" >" + jsonObj.name + "</option>";

        }
        $("#province").html("<option value='请选择'>请选择...</option> " + optionstring);
        $('.selectpicker').selectpicker('refresh');
      /*  $("#province").selectpicker({
        });*/
    });


    $.get("../hesequipment/findListByParam/", function (r) {
        // vm.provinces = r.region;
        var data = r.hesEquipmentList;
        var optionstring = "";
        for (var i in data) {
            var jsonObj = data[i];

            optionstring += "<option value=\"" + jsonObj.code + "\" >" + jsonObj.name + "</option>";

        }
        $("#devicetype").html("<option value='请选择'>请选择...</option> " + optionstring);
    });


    $.get("/zwan/companyinfo/companyList/", function (r) {
        var data = r.data;
        var optionstring = "";
        for (var i in data) {
            var jsonObj = data[i];

            optionstring += "<option value=\"" + jsonObj.COMPANYID + "\" >" + jsonObj.COMPANYNAME + "</option>";

        }
        $("#companyId").html("<option value='请选择'>请选择...</option> " + optionstring);
    });

});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        hesUser: {},
        hesExpert: {}
    },
    methods: {
        query: function () {
            //vm.reload();
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    keyword: $("#keyword").val()
                },
                page: 1
            }).trigger("reloadGrid");
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.hesUser = {};
        },
        update: function (event) {
            var userId = getSelectedRow();
            if (userId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(userId)
        },
        hesexpert: function () {
            $('#addExpertModal').modal({
                keyboard: true
            });

        },
        saveOrUpdate: function (event) {
            var url = vm.hesUser.userId == null ? "../hesuser/save" : "../hesuser/update";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.hesUser),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        addexpert: function (event) {
            var rowData = $("#jqGrid").getRowData(getSelectedRow());
            var hes = {
                adept: $("#devicetype").val(),
                citycode: $("#cities").val(),
                companyId: $("#companyId").val()
            };

            var json = Object.assign(rowData, hes);
            console.log(json);
            var url = "../hesuser/addExpert";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(json),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            $('#addExpertModal').modal('hide');
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var userIds = getSelectedRows();
            if (userIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../hesuser/delete",
                    data: JSON.stringify(userIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getcities: function (provinceid) {
            $.get("../hesexpert/cities/" + provinceid, function (r) {
                // vm.cities = r.cities;
                var data = r.cities;
                var optionstring = "";
                for (var i in data) {
                    var jsonObj = data[i];


                    optionstring += "<option value=\"" + jsonObj.citycode + "\" >" + jsonObj.name + "</option>";


                }
                $("#cities").html("<option value='请选择'>请选择...</option> " + optionstring);
                $('.selectpicker').selectpicker('refresh');
            });


        },

        getInfo: function (userId) {
            $.get("../hesuser/info/" + userId, function (r) {
                vm.hesUser = r.hesUser;
                //更新输入框高度
                autosize.update($('textarea'));
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        syc: function () {
            $.get("../hesuser/batchsave", function (r) {
                if (r.code == 0) {
                    alert('操作成功');
                } else {
                    alert(r.msg);
                }
            });
        },
        sysadmin: function () {
            var rowData = $("#jqGrid").getRowData(getSelectedRow());
            if (rowData != null && rowData != '') {
                var url = "../hesuser/sysadmin";
                $.ajax({
                    type: "POST",
                    dataType: "jsonp",
                    url: url,
                    data: JSON.stringify(rowData),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }


        },
        extend: function (des, src, override) {
            if (src instanceof Array) {
                for (var i = 0, len = src.length; i < len; i++)
                    vm.extend(des, src[i], override);
            }
            for (var i in src) {
                if (override || !(i in des)) {
                    des[i] = src[i];
                }
            }
            return des;
        }, test: function () {
            var data = {
                companyId: "",
                orderId: "testOrder",
                userName: ""
            };
            $.ajax({
                type: "post",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                url: "http://122.112.226.39:8082/topvision-api/video/findListByParam",
                data: JSON.stringify(data),
                success: function (r) {
                    if (r.code === 0) {
                        console.log(r);
                    } else {
                        alert(r.msg);
                    }
                }
            });

        }

    }
});

// 图片自适应


// textarea 高度自动扩展
autosize($('textarea'));